package com.ruoyi.gbrz.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.gbrz.domain.GbrzFj;
import com.ruoyi.gbrz.mapper.GbrzMapper;
import com.ruoyi.gbrz.domain.Gbrz;
import com.ruoyi.gbrz.service.IGbrzService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 贯标认证Service业务层处理
 *
 * @author huacai
 * @date 2024-10-26
 */
@Service
public class GbrzServiceImpl implements IGbrzService {
    @Resource
    private GbrzMapper gbrzMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询贯标认证
     *
     * @param gbrzId 贯标认证主键
     * @return 贯标认证
     */
    @Override
    public Gbrz selectGbrzByGbrzId(String gbrzId) {
        return gbrzMapper.selectGbrzByGbrzId(gbrzId);
    }

    /**
     * 查询贯标认证列表
     *
     * @param gbrz 贯标认证
     * @return 贯标认证
     */
    @Override
    public List<Gbrz> selectGbrzList(Gbrz gbrz) {
        return gbrzMapper.selectGbrzList(gbrz);
    }

    /**
     * 新增贯标认证
     *
     * @param gbrz 贯标认证
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGbrz(Gbrz gbrz) {
        gbrz.setCreateTime(DateUtils.getNowDate());
        gbrz.setIdWhere("存在");
        int rows = gbrzMapper.insertGbrz(gbrz);
        insertGbrzFj(gbrz);
        return rows;
    }

    /**
     * 批量新增贯标认证
     *
     * @param gbrzs 贯标认证List
     * @return 结果
     */
    @Override
    public int batchInsertGbrz(List<Gbrz> gbrzs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(gbrzs)) {
            try {
                for (int i = 0; i < gbrzs.size(); i++) {
                    int row = gbrzMapper.insertGbrz(gbrzs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == gbrzs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改贯标认证
     *
     * @param gbrz 贯标认证
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGbrz(Gbrz gbrz) {
        gbrz.setUpdateTime(DateUtils.getNowDate());
//        gbrzMapper.deleteGbrzFjByGbrzId(gbrz.getGbrzId());
        insertGbrzFj(gbrz);
        return gbrzMapper.updateGbrz(gbrz);
    }

    /**
     * 批量删除贯标认证
     *
     * @param gbrzIds 需要删除的贯标认证主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGbrzByGbrzIds(String[] gbrzIds) {
        gbrzMapper.deleteGbrzFjByGbrzIds(gbrzIds);
        return gbrzMapper.deleteGbrzByGbrzIds(gbrzIds);
    }

    /**
     * 删除贯标认证信息
     *
     * @param gbrzId 贯标认证主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGbrzByGbrzId(String gbrzId) {
        gbrzMapper.deleteGbrzFjByGbrzId(gbrzId);
        return gbrzMapper.deleteGbrzByGbrzId(gbrzId);
    }

    /**
     * 新增附件
     * @param gbrzFj
     * @return
     */
    @Override
    public int insertGbrzFjByGbrzId(GbrzFj gbrzFj) {
        return gbrzMapper.insertGbrzFjByGbrzId(gbrzFj);
    }

    /**
     * 根据ID查询附件列表
     * @param gbrzFj
     * @return
     */
    @Override
    public List<GbrzFj> selectGbrzFjList(GbrzFj gbrzFj) {
        return gbrzMapper.selectGbrzFjList(gbrzFj);
    }

    /**
     * 批量删除贯标认证附件
     *
     * @param fjIds 需要删除的贯标认证附件主键
     * @return 结果
     */
    @Override
    public int deleteGbrzFjByFjIds(Integer[] fjIds)
    {
        return gbrzMapper.deleteGbrzFjByFjIds(fjIds);
    }

    /**
     * 新增贯标认证附件信息
     *
     * @param gbrz 贯标认证对象
     */
    public void insertGbrzFj(Gbrz gbrz) {
        List<GbrzFj> gbrzFjList = gbrz.getGbrzFjList();
        String gbrzId = gbrz.getGbrzId();
        if (StringUtils.isNotNull(gbrzFjList)) {
            List<GbrzFj> list = new ArrayList<GbrzFj>();
            for (GbrzFj gbrzFj : gbrzFjList) {
                gbrzFj.setGbrzId(gbrzId);
                list.add(gbrzFj);
            }
//            if (list.size() > 0) {
//                gbrzMapper.batchGbrzFj(list);
//            }
        }
    }
}
